<?php
/**
 * 城镇中心
 * 
 * Created	: 2011-05-18
 * Modified : 2011-05-18
 * @author  : 陈立强
 */
class Town
{
	
	/**
	 * 作物种子商店获取列表
	 * @param int $uid
	 */
	public static function getCropSeedList($uid)
	{
		global $DB, $L, $G;
		
		$cfgList = C('item/crop');
		$skillNameList = C('item/skill_type_name');
		
		$list = array();
		foreach ($cfgList as $id=>$item) {
			$list[$item['seed_id']] = array(
				'id'		=> $id,
				'item_id'	=> $item['seed_id'],
				'title'		=> $item['seed_title'],
				'price'		=> $item['seed_price'],
				'lvl'		=> $item['lvl'],
				'skill_type'=> $skillNameList[$item['skill_type']],
				'product_title'	=> $item['title'],
				'plant_season'	=> $item['plant_season'],
				'plant_need_time'	=> $item['plant_need_time']
			);
		}
		
		return array(
			'val'  => true,
			'data' => $list
		);
	}
	
	/**
	 * 果苗商店获取列表
	 * @param int $uid
	 */
	public static function getFruitSeedList($uid)
	{
		global $DB, $L, $G;
		
		$cfgList = C('item/fruit');
		$skillNameList = C('item/skill_type_name');
		
		$list = array();
		foreach ($cfgList as $id=>$item) {
			$list[$item['seed_id']] = array(
				'id'		=> $id,
				'item_id'	=> $item['seed_id'],
				'title'		=> $item['seed_title'],
				'price'		=> $item['seed_price'],
				'lvl'		=> $item['lvl'],
				'skill_type'=> $skillNameList[$item['skill_type']],
				'product_title'	=> $item['title'],
				'plant_season'	=> $item['plant_season'],
				'grow_need_time'=> $item['grow_need_time'],
				'ripe_need_time'=> $item['ripe_need_time'],
				'harvest_times'	=> $item['harvest_times'],
			);
		}
		
		return array(
			'val'  => true,
			'data' => $list
		);
	}
	
	/**
	 * 动物商店获取列表
	 * @param int $uid
	 */
	public static function getAnimalList($uid)
	{
		global $DB, $L, $G;
		
		$cfgList = C('item/animal');
		$skillNameList = C('item/skill_type_name');
		
		$list = array();
		foreach ($cfgList as $id=>$item) {
			$list[$item['seed_id']] = array(
				'id'		=> $id,
				'item_id'	=> $item['seed_id'],
				'title'		=> $item['seed_title'],
				'price'		=> $item['seed_price'],
				'lvl'		=> $item['lvl'],
				'skill_type'=> $skillNameList[$item['skill_type']],
				'product_title'	=> $item['title'],
				'grow_need_time'=> $item['grow_need_time'],
				'ripe_need_time'=> $item['ripe_need_time'],
				'harvest_times'	=> $item['harvest_times'],
				'food_val_reduce_need_seconds'	=> $item['food_val_reduce_need_seconds'],
			);
		}
		
		return array(
			'val'  => true,
			'data' => $list
		);
	}
	
	/**
	 * 辅材商店获取列表
	 * @param int $uid
	 */
	public static function getsecMatList($uid)
	{
		global $DB, $L, $G;
		
		C('build/making');
		
		$list = array();
		foreach ($G['make_class_type_sign_list'] as $sign) {
			$cfgList = $G[$sign.'_sec_mat_item'];
			foreach ($cfgList as $item_id=>$item) {
				for ($quality=1; $quality<=3; $quality++) {
					$qualitySign = $G['id_quality'][$quality];
					$list[] = array(
						'item_id'	=> $item_id,
						'title'		=> $item['title'].$qualitySign,
						'price'		=> $item['price'],
						'type'		=> $sign,
						'quality'	=> $quality
					);
				}
			}
		}
		
		return array(
			'val'  => true,
			'data' => $list
		);
	}
	
	/**
	 * 购买作物种子
	 * @param int $uid
	 * @param int $item_id
	 * @param int $num
	 */
	private static function buyCropSeed($uid, $item_id, $num)
	{
		global $DB, $L, $G;
		
		$cfgList = C('item/item_list');
		$cfg = $cfgList[$item_id];
		// 金钱是否足够
		$needCoin = $cfg['price']*$num;
		$sql = 'SELECT coin FROM user WHERE uid='.$uid;
		$coin = $DB->getValue($sql);
		if ($needCoin > $coin) {
			return array(
				'val' => false,
				'msg' => $L['coin_not_enough']
			);
		}
		
		$data = array(
			'item_id'	=> $item_id,
			'item_type'	=> 2,
			'title'		=> $cfg['title'],
			'num'		=> $num,
			'stack'		=> 1,
			'quality'	=> 1,
			'lvl'		=> $cfg['lvl']
		);
		$ret = Depot::in($uid, $data);
		if (!$ret['val']) {
			$DB->rollBack();
			return $ret;
		}
		
		// 扣除金线
		if ($ret) {
			$sql = 'UPDATE user set coin=coin-'.$needCoin.' WHERE uid='.$uid;
			$ret = $DB->exeSql($sql);
		}
		
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
			
		} else {
			$result['msg'] = $L['Failure_Update'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('town_buy_crop_seed', $str);
		}

		return $result;
	}
	
	/**
	 * 购买果苗
	 * @param int $uid
	 * @param int $item_id
	 * @param int $num
	 */
	private static function buyFruitSeed($uid, $item_id, $num)
	{
		global $DB, $L, $G;
		
		$cfgList = C('item/item_list');
		$cfg = $cfgList[$item_id];
		// 金钱是否足够
		$needCoin = $cfg['price']*$num;
		$sql = 'SELECT coin FROM user WHERE uid='.$uid;
		$coin = $DB->getValue($sql);
		if ($needCoin > $coin) {
			return array(
				'val' => false,
				'msg' => $L['coin_not_enough']
			);
		}
		
		$data = array(
			'item_id'	=> $item_id,
			'item_type'	=> 4,
			'title'		=> $cfg['title'],
			'num'		=> $num,
			'stack'		=> 1,
			'quality'	=> 1,
			'lvl'		=> $cfg['lvl']
		);
		$ret = Depot::in($uid, $data);
		if (!$ret['val']) {
			$DB->rollBack();
			return $ret;
		}
		
		// 扣除金线
		if ($ret) {
			$sql = 'UPDATE user set coin=coin-'.$needCoin.' WHERE uid='.$uid;
			$ret = $DB->exeSql($sql);
		}
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
			
		} else {
			$result['msg'] = $L['Failure_Update'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('town_buy_fruit_seed', $str);
		}

		return $result;
	}
	
	/**
	 * 购买动物幼仔
	 * @param int $uid
	 * @param int $item_id
	 * @param int $num
	 */
	private static function buyAnimal($uid, $item_id, $num)
	{
		global $DB, $L, $G;
		
		$cfgList = C('item/item_list');
		$cfg = $cfgList[$item_id];
		// 金钱是否足够
		$needCoin = $cfg['price'] * $num;
		$sql = 'SELECT coin FROM user WHERE uid='.$uid;
		$coin = $DB->getValue($sql);
		if ($needCoin > $coin) {
			return array(
				'val' => false,
				'msg' => $L['coin_not_enough']
			);
		}
		
		$data = array(
			'item_id'	=> $item_id,
			'item_type'	=> 6,
			'title'		=> $cfg['title'],
			'num'		=> $num,
			'stack'		=> 1,
			'quality'	=> 1,
			'lvl'		=> $cfg['lvl']
		);
		$ret = Depot::in($uid, $data);
		
		// 扣除金线
		if ($ret) {
			$sql = 'UPDATE user set coin=coin-'.$needCoin.' WHERE uid='.$uid;
			$ret = $DB->exeSql($sql);
		}
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
			
		} else {
			$result['msg'] = $L['Failure_Update'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('town_buy_fruit_seed', $str);
		}

		return $result;
	}
	
	/**
	 * 购买辅材
	 * @param int $uid
	 * @param int $item_id
	 * @param int $num
	 */
	public static function buysecMat($uid, $item_id, $num, $quality)
	{
		global $DB, $L, $G;
		
		$cfgList = C('item/item_list');
		$cfg = $cfgList[$item_id];
		// 金钱是否足够
		$needCoin = $cfg['price']*$num;
		$sql = 'SELECT coin FROM user WHERE uid='.$uid;
		$coin = $DB->getValue($sql);
		if ($needCoin > $coin) {
			return array(
				'val' => false,
				'msg' => $L['coin_not_enough']
			);
		}
		
		$qualitySign = $G['id_quality'][$quality];
		$data = array(
			'item_id'	=> $item_id,
			'item_type'	=> 51,
			'title'		=> $cfg['title'].$qualitySign,
			'num'		=> $num,
			'stack'		=> 1,
			'quality'	=> $quality,
			'lvl'		=> $cfg['lvl']
		);
		$ret = Depot::in($uid, $data);
		if (!$ret['val']) {
			$DB->rollBack();
			return $ret;
		}
		
		// 扣除金线
		if ($ret) {
			$sql = 'UPDATE user set coin=coin-'.$needCoin.' WHERE uid='.$uid;
			$ret = $DB->exeSql($sql);
		}
		
		$result['val'] = (boolean)$ret;
		if ($ret) {
			
		} else {
			$result['msg'] = $L['Failure_Update'];
			$str = var_export($DB->errors, true);
			// 记录错误日志
			Log::save('town_buy_fruit_seed', $str);
		}

		return $result;
	}
	
	
}










